package com.cskaoyan.controller;

import com.cskaoyan.bean.goods.Bo.goodsUpdate.GoodsUpdateBo;
import com.cskaoyan.bean.goods.Bo.goodsCreate.GoodsCreateBo;
import com.cskaoyan.bean.goods.Bo.goodsUpdate.GoodsUpdateOfGoodsBo;
import com.cskaoyan.bean.goods.vo.GoodsVo;
import com.cskaoyan.bean.goods.vo.catVo.CatAndBrandVo;
import com.cskaoyan.bean.goods.vo.goods.GoodsCreateVo;
import com.cskaoyan.bean.goods.vo.goods.GoodsListVo;
import com.cskaoyan.bean.goods.Bo.BaseParamBo;
import com.cskaoyan.bean.goods.vo.detail.GoodsDetailVo;
import com.cskaoyan.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @Author: 蒋镇泽
 * @Description:
 * @Date: 2022-06-05-15:20
 */
@RestController
@RequestMapping("admin/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @GetMapping("catAndBrand")
    public CatAndBrandVo catAndBrand() {
        CatAndBrandVo catAndBrandVo = goodsService.queryCatAndBrand();
        return catAndBrandVo;
    }

    @GetMapping("detail")
    public GoodsDetailVo detail(Integer id) {
        GoodsDetailVo goodsDetailVo = goodsService.queryGoodsDetail(id);
        return goodsDetailVo;
    }

    @GetMapping("list")
    public GoodsListVo list(BaseParamBo baseParam) {
        GoodsListVo goodsListVo = goodsService.query(baseParam);
        return goodsListVo;
    }


    @PostMapping("create")
    public GoodsCreateVo create(@RequestBody GoodsCreateBo goodsCreateBo) {
        GoodsCreateVo goodsCreateVo = new GoodsCreateVo();
        try {
            goodsService.insertGoods(goodsCreateBo);
            goodsCreateVo = new GoodsCreateVo(0, "成功");
        } catch (Exception e) {
            goodsCreateVo = new GoodsCreateVo(404, "插入商品失败");
        }

        return goodsCreateVo;
    }

    @RequestMapping("delete")
    public GoodsVo delete(@RequestBody GoodsUpdateOfGoodsBo goodsdeleteOfGoodsBo) {
        goodsService.deleteGoods(goodsdeleteOfGoodsBo);
        return new GoodsVo(0, "成功");
    }

    @RequestMapping("update")
    public GoodsVo update(@RequestBody GoodsUpdateBo goodsDetailVo) {

        goodsService.updateGoods(goodsDetailVo);
        GoodsVo goodsVo = new GoodsVo(0, "成功");
        return goodsVo;
    }
}
